﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OASys.Web.Areas.ModuleSys.Models;
using OASys.Entity;
using OASys.Biz.BizAccess;
using OASys.Enum;
using Project.Common;
using OASys.Biz.BizAccess;
using OASys.Web.Areas.ModuleSys.Models;

namespace OASys.Web.Areas.ModuleSys.Controllers
{
    /// <summary>
    /// 工作日设定
    /// 作者：尤啸
    /// 时间：2012-05-25
    /// </summary>
    public class WorkDayDateDiffController : Controller
    {
        private BizSysDate bizSysDate;
        //
        // GET: /ModuleSys/YouXiaoWorkDayDateDiff/
        public ActionResult Index(ModelWorkDayDateDiffIndex model)
        {
            model.RetriveData();
            return View(model);
        }
        public ActionResult Create(ModelWorkDayDateDiffCreate model)
        {
            ViewBag.PageState = model.PageState;
            model.RetriveDate();
            return View(model);
        }
        /// <summary>
        /// 查出数据表中所有的天
        /// </summary>
        /// <param name="changeDate"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetSelectDay(DateTime? changeDate, string type)
        {
            JsonResult jsresult = new JsonResult();
            jsresult.ContentType = Consts.CONTENT_TYPE;

            if (type == System.Enum.GetName(typeof(EnumClanderType)
                                        , EnumClanderType.current))
            {

            }
            else if (type == System.Enum.GetName(typeof(EnumClanderType)
                                        , EnumClanderType.next))
            {
                changeDate = changeDate.Value.AddYears(1);
            }
            else if (type == System.Enum.GetName(typeof(EnumClanderType)
                                        , EnumClanderType.prev))
            {
                changeDate = changeDate.Value.AddYears(-1);
            }


            /* 判断数据库是否有要改年的数据 如果没有则添加
            ----------------------------------------------------------*/
            string result = GenerateDay(changeDate.Value);
            if (result != string.Empty)
            {
                jsresult.Data = new { result = result };
                return jsresult;
            }
            IList<SysDate> sysDateCollection = bizSysDate.ListBy(x => x.IsHoliday == true && x.Date.Value.Year == changeDate.Value.Year);

            IList<int> list = sysDateCollection.Select(x => x.Date.Value.Day).ToList();

            Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>();

            IList<int> lstMonth = new List<int>();
            lstMonth = sysDateCollection.Select(x => x.Date.Value.Month).Distinct().OrderBy(x => x).ToList();

            foreach (int month in lstMonth)
            {
                List<int> lstDay = new List<int>();
                foreach (SysDate date in sysDateCollection)
                {
                    if (month == date.Date.Value.Month)
                    {
                        lstDay.Add(date.Date.Value.Day);
                    }
                }
                dict.Add(month, lstDay);
            }

            jsresult.Data = new { result = dict.ToArray() };
            return jsresult;

        }
        public string GenerateDay(DateTime currentDate)
        {
            /* 判断数据库是否有要改年的数据 如果没有则添加
            ----------------------------------------------------------*/
            if (bizSysDate.CountBy(x => x.Date.Value.Year == currentDate.Year) > 0)
            {
                return string.Empty;
            }

            return bizSysDate.GenerateDay(currentDate);
        }

        [HttpPost]
        public ActionResult ChangeSelectDayStatus(DateTime changeDate)
        {
            JsonResult jsresult = new JsonResult();
            jsresult.ContentType = Consts.CONTENT_TYPE;

            try
            {
                SysDate sysDateEntity = bizSysDate.GetFirst(x => x.Date.Value.Year == changeDate.Year && x.Date.Value.Month == changeDate.Month && x.Date.Value.Day == changeDate.Day);
                sysDateEntity.IsHoliday = sysDateEntity.IsHoliday.Value ? false : true;
                bizSysDate.SaveOrUpdate(sysDateEntity);
                jsresult.Data = new { result = string.Empty };
            }
            catch (Exception ex)
            {
                jsresult.Data = new { result = OASys.Resources.Properties.Resources.M00002E };
                Log4netHelper.Logger.Fatal(ex);
            }

            return jsresult;
        }
    }
}
